<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class Order_model extends MY_Model {

	var $table = 'order';
	private $CI;

	public function __construct() {
		parent::__construct();
		$this->CI = & get_instance();
	}

	public function getOrderAll($config = array(), $like = array()) {
		$config['totalCount'] = $this->get_search_num(NULL, $like);
		if ($config['offset'] >= $config['totalCount']) {
			$config['offset'] = 0;
			$config['pageNum'] = 1;
		}
		$config['field'] = 'o.id';
		$config['select'] = 'o.id,o.order_num,o.netpay,o.pay_method,o.pay_time,o.contact_address,o.note,u.username as user_username,a.username as diliverier_username,group_concat(a1.username separator \'|\') as agent_username';
		$config['join'] = array(
			array('user u', 'o.user_id=u.id', 'left'),
			array('agent a', 'o.agent_id=a.id', 'left'),
			array('agent_assignment aa', 'o.address_id=aa.address_id', 'left'),
			array('agent a1', 'aa.agent_id=a1.id', 'left')
		);
		$config['group_by'] = 'o.id';
		$config['data'] = $this->search(NULL, $config, $like, false, ' o');
		return $config;
	}

	public function overtimePack($scheduled_overtime) {
		$query = $this->db->select('o.order_num,o.pay_time as overtime,group_concat(ag.username separator \'|\') as username, concat_ws(\' \',a.description,ae.address) as address', false)
				->from($this->table . ' o')
				->join('agent_assignment aa', 'o.address_id=aa.address_id', 'left')
				->join('agent ag', 'aa.agent_id=ag.id', 'left')
				->join('areas_enabled ae', 'o.address_id=ae.id', 'left')
				->join('areas a', 'ae.district_id=a.id', 'left')
				->where_in('o.status', array(REST_Controller::PAY_STATUS_PAYED, REST_Controller::PAY_STATUS_CASH, REST_Controller::PAY_STATUS_WAIT_DELIVER))
				->where(array(
					'o.agent_id' => 0,
					'o.pay_time <' => time() - $scheduled_overtime,
					'ag.role' => REST_Controller::ROLE_AGENT))
				->group_by('o.id')
				->get();
		return $query->result_array();
	}

	public function refundPack() {
		$query = $this->db->select('o.order_num,o.created_at,ag.username,concat_ws(\' \',a.description,ae.address) as address', false)
				->from($this->table . ' r')
				->join('order o', 'r.order_id=o.id', 'left')
				->join('agent ag', 'r.agent_id=ag.id', 'left')
				->join('areas_enabled ae', 'o.address_id=ae.id', 'left')
				->join('areas a', 'ae.district_id=a.id', 'left')
				->where(array('o.status' => REST_Controller::PAY_STATUS_REFUNDING,
					'r.status' => REST_Controller::STATUS_ACTIVE))
				->group_by('o.id')
				->get();
		return $query->result_array();
	}

	public function refund($ids, $batch_no) {
		if (empty($ids)) {
			return false;
		}
		$this->lang->load('code');
		try {
			$this->db->trans_begin();
			foreach ($ids as $v) {
				$data = array(
					'status' => REST_Controller::PAY_STATUS_REFUNDED,
					'updated_at' => time()
				);
				$where = array(
					'status' => REST_Controller::PAY_STATUS_REFUNDING,
					'id' => $v,
				);
				$this->db->update($this->table, $data, $where);
				$result = $this->db->affected_rows();
				if(!$result) {
					throw new Exception('Syntax error');
				}
				$data = array(
					'order_id' => $v,
					'title' => $this->lang->line("w10009"),
					'short_desc' => '',
					'status' => '9',
					'type' => '3',
					'created_at' => time()
				);
				$this->db->insert('order_log', $data);
				$result = $this->db->insert_id();
				if(!$result) {
					throw new Exception('Syntax error');
				}
			}
			$data = array(
				'status' => REST_Controller::STATUS_DELETE,
				'refund_time' => time(),
				'ext_refund_num' => $batch_no
			);
			$this->db->where(array('refund_num'=>$batch_no, 'status'=>  REST_Controller::STATUS_ACTIVE));
			$this->db->where_in('order_id', $ids);
			$this->db->update('refund', $data);
			$result = $this->db->affected_rows();
			if(!$result || !$this->db->trans_status()) {
				throw new Exception('Syntax error');
			}
			$this->db->trans_commit();
			return true;
		} catch (Exception $e) {
			$this->db->trans_rollBack();
			return false;
		}
	}

}
